﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using seejee_Backendium.Core.Interfaces;
using seejee_Backendium.Core.Models;

namespace seejee_Backendium.Data.Services
{
    public class UserInfoService:IUserInfoService
    {
        private readonly string _connectionString;
        private readonly ILogger<UserInfoService> _logger;

        public UserInfoService(IConfiguration config, ILogger<UserInfoService> logger)
        {
            _connectionString = config.GetConnectionString("Default");
            _logger = logger;
        }

        public async Task<IEnumerable<UserReginfo>> GetAllAsync()
        {
            using var connection = new SqlConnection(_connectionString);
            await connection.OpenAsync();

            try
            {
                // 直接查询所有字段（与表结构一致）
                var dataSources = await connection.QueryAsync<UserReginfo>(
                    "select c.cCusName,c.cCusUserKey,l.dCusExpiryDate,u.cImage from CusUserList c inner join Licenses l on c.cCusName = l.cCusName inner join SEBI_UserReginfo u on c.cCusName = u.cRefName where u.cid = 1");
                return dataSources;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取所有UserReginfo记录异常");
            }
            finally
            {
                await connection.CloseAsync();
            }
            return Enumerable.Empty<UserReginfo>(); // 返回空集合而非null，避免控制器空引用
        }

        public async Task<IEnumerable<UserinfoForGet>> GetCusForGetAsync()
        {
            using var connection = new SqlConnection(_connectionString);
            await connection.OpenAsync();

            try
            {
                // 直接查询所有字段（与表结构一致）
                var dataSources = await connection.QueryAsync<UserinfoForGet>(
                    "select c.cCusName,c.cCusUserKey from CusUserList c inner join Licenses l on c.cCusName = l.cCusName inner join SEBI_UserReginfo u on c.cCusName = u.cRefName where u.cid = 1");
                return dataSources;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取所有UserinfoForGet记录异常");
            }
            finally
            {
                await connection.CloseAsync();
            }
            return Enumerable.Empty<UserinfoForGet>(); // 返回空集合而非null，避免控制器空引用
        }
    }
}
